package cn.joeg.app.web.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class ZipOut {

    private static Logger logger = LoggerFactory.getLogger(ZipOut.class);

    public static byte[] gzip(final String input) {
        if (StringUtils.isEmpty(input)) {
            return null;
        }
//        logger.debug("out: " + input);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(input.getBytes());
            gzip.finish();
            gzip.flush();
            return out.toByteArray();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            try {
                gzip.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
        return null;
    }

}
